package com.funtl.screw.admin.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funtl.screw.admin.entity.SysRole;
import com.funtl.screw.admin.vo.RoleVO;
import com.funtl.screw.admin.service.SysRoleService;
import com.funtl.screw.common.core.constant.CacheConstants;
import com.funtl.screw.common.core.util.QueryRequest;
import com.funtl.screw.common.core.util.ResponseResult;
import com.funtl.screw.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Lusifer
 * @since 2020-02-10
 */
@RestController
@AllArgsConstructor
@RequestMapping("/role")
@Api(value = "role", tags = "角色管理模块")
public class SysRoleController {

    private final SysRoleService sysRoleService;

    /**
     * 通过ID查询角色信息
     *
     * @param id ID
     * @return 角色信息
     */
    @GetMapping("/{id}")
    public ResponseResult<SysRole> getById(@PathVariable Long id) {
        return ResponseResult.ok(sysRoleService.getById(id));
    }

    /**
     * 通过角色编码查询角色信息
     *
     * @param code 角色Code
     * @return 角色信息
     */
    @GetMapping("/code/{code}")
    public ResponseResult<List<SysRole>> getByRoleCode(@PathVariable String code) {
        return ResponseResult.ok(sysRoleService.list(Wrappers.<SysRole>lambdaQuery().eq(SysRole::getRoleCode, code)));
    }

    /**
     * 添加角色
     *
     * @param sysRole 角色信息
     * @return success、false
     */
    @SysLog("添加角色")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sys_role_add')")
    @CacheEvict(value = CacheConstants.ROLE_DETAILS, allEntries = true)
    public ResponseResult<Boolean> save(@Valid @RequestBody SysRole sysRole) {
        return ResponseResult.ok(sysRoleService.save(sysRole));
    }

    /**
     * 修改角色
     *
     * @param sysRole 角色信息
     * @return success/false
     */
    @SysLog("修改角色")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sys_role_edit')")
    @CacheEvict(value = CacheConstants.ROLE_DETAILS, allEntries = true)
    public ResponseResult<Boolean> update(@Valid @RequestBody SysRole sysRole) {
        return ResponseResult.ok(sysRoleService.updateById(sysRole));
    }

    /**
     * 删除角色
     */
    @SysLog("删除角色")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sys_role_del')")
    @CacheEvict(value = CacheConstants.ROLE_DETAILS, allEntries = true)
    public ResponseResult<Boolean> removeById(@PathVariable Long id) {
        return ResponseResult.ok(sysRoleService.removeRoleById(id));
    }

    /**
     * 获取角色列表
     *
     * @return 角色列表
     */
    @GetMapping("/list")
    public ResponseResult<List<SysRole>> listRoles() {
        return ResponseResult.ok(sysRoleService.list(Wrappers.emptyWrapper()));
    }

    /**
     * 分页查询角色信息
     */
    @GetMapping("/page")
    public ResponseResult<Page<SysRole>> getRolePage(QueryRequest queryRequest, SysRole role) {
        Page<SysRole> page = new Page<>(queryRequest.getCurrent(), queryRequest.getSize());
        return ResponseResult.ok(sysRoleService.page(page, Wrappers.query(role)));
    }

    /**
     * 更新角色菜单
     *
     * @param roleVo 角色对象
     * @return success、false
     */
    @SysLog("更新角色菜单")
    @PutMapping("/menu")
    @PreAuthorize("@pms.hasPermission('sys_role_perm')")
    public ResponseResult<Boolean> saveRoleMenus(@RequestBody RoleVO roleVo) {
        return ResponseResult.ok(sysRoleService.updateRoleMenus(roleVo));
    }

    /**
     * 通过角色ID 查询角色列表
     */
    @PostMapping("/getRoleList")
    public ResponseResult<List<SysRole>> getRoleList(@RequestBody List<Long> roleIdList) {
        return ResponseResult.ok(sysRoleService.findRolesByRoleIds(roleIdList, CollUtil.join(roleIdList, StrUtil.UNDERLINE)));
    }
}
